home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT63.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  11.2 KB  |  475 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 63                         
  5.                                           
  6.  This is the same as example 62 except for the fact that it runs in                                                                              
  7.  VESA using 640x480x256. A VESA driver is required of course.....
  8.  
  9.  *** PROJECT ***                                                             
  10.  This program requires the WGT5_WC.LIB, WVESA_WC.LIB and WSCR_WC.LIB files 
  11.  to be linked.   
  12.                                           
  13.  *** DATA FILES ***                                                          
  14.  MUN.WMP, MUNCHMAP.SPR, MUNCHKIN.SPR                                         
  15.                                WATCOM C++ VERSION 
  16. ==============================================================================
  17. */
  18. #include <dos.h>
  19. #include <malloc.h>
  20. #include <stdio.h>
  21. #include <conio.h>
  22.  
  23. #include <wgt5.h>
  24. #include <wgtscrol.h>
  25. #include <wgtvesa.h>
  26.  
  27. #define _XSIZE_ 640
  28. #define _YSIZE_ 480
  29. #define DESIRED_MODE V640x480
  30.  
  31.  
  32. typedef short tiletypes[256];
  33.  
  34.  
  35. #define YOU 37
  36. void checkfeet (void);
  37. void checkhead (void);
  38. void checkright (void);
  39. void checkleft (void);
  40. void findelevators (void);
  41. void upelev (void);
  42. void downelev (void);
  43. void checkelevators (void);
  44. void moveguys (void);
  45.  
  46. short ox,oy,dir,anim;
  47. short jumping,addy;
  48. short spx,spy;
  49.  
  50. int timer;
  51. // The number of times the screen is updated
  52. int updates;
  53.  
  54. short feet1,feet2,head1,head2;
  55. short windx,windy;
  56.  
  57. wgtmap munchmap;                        // our world map
  58. tiletypes munchtypes;
  59.  
  60. #define UP 72
  61. #define DOWN 80
  62. #define LEFT 75
  63. #define RIGHT 77
  64. #define CTRL 29
  65. #define ESC 1
  66.  
  67. #define NUM_SPR 51
  68. #define NUM_TILE 255
  69. #define NUM_OBJ 101
  70. #define TILE_SIZE 16
  71.  
  72. #define MAINWIN 0
  73.  
  74. color palette[256];             // our palette of colours
  75.  
  76. block munchtiles[NUM_TILE];             // our blocks for the map
  77. block munchsprites[NUM_SPR];            // our sprites 
  78. block screenbuf;
  79. scrollsprite munchobj[NUM_OBJ];
  80.  
  81. short i;
  82.  
  83. typedef struct {
  84.     short curheight;
  85.     short origy,origx;
  86.     short timer;
  87.     } elevator;
  88.  
  89. short replace[200];
  90. short elevup=-1;
  91. short oldmode;
  92.  
  93. elevator elev[30];
  94. short numelev=0;
  95.  
  96.  
  97. void timerctr (void)
  98. {
  99.   timer++;
  100. }
  101.  
  102.  
  103. void main (void)
  104. {
  105.   oldmode = wgetmode ();
  106.   if (!vgadetected ())
  107.   {
  108.     printf ("VGA is required to run this program...");
  109.     exit (1);
  110.   }
  111.  
  112.   printf ("WGT Example #63\n\n");
  113.   printf ("8-WAY SCROLLING SVGA DEMO\n");
  114.   printf ("Arrow keys move, CTRL jumps. Up/down looks in direction or operates\n");
  115.   printf ("elevators.  ESC quits the program.\n");
  116.   printf ("\n\nWindow Width (2-40):");
  117.   scanf ("%i", &windx);
  118.   printf ("\nWindow Height (2-24):");
  119.   scanf ("%i", &windy);
  120.  
  121.   vga256 ();
  122.   if (wvesa_detected ())
  123.   {
  124.     wvesa_getmodeinfo (DESIRED_MODE);
  125.     wvesa_init (DESIRED_MODE);
  126.   }
  127.   wloadsprites (palette, "munchmap.spr", munchtiles, 0, NUM_TILE - 1);
  128.   wloadsprites (palette, "munchkin.spr", munchsprites, 0, NUM_SPR - 1);
  129.   wsetpalette (0, 255, palette);
  130.   
  131.   screenbuf = wallocblock (_XSIZE_, _YSIZE_);
  132.   wsetscreen (screenbuf);
  133.   
  134.   winitscroll (MAINWIN, NORMAL, - 1, windx, windy, munchtiles);
  135.   memset (&munchobj[0], 0, sizeof (scrollsprite)*NUM_OBJ);
  136.   munchmap = wloadmap (MAINWIN, "mun.wmp", munchtypes, munchobj);
  137.   
  138.   findelevators ();
  139.   
  140.   wshowwindow (MAINWIN, 0, 0);
  141.   
  142.   installkbd ();
  143.   
  144.   munchobj[YOU].on = 1;
  145.   munchobj[YOU].x = 16;
  146.   munchobj[YOU].y = 242;
  147.   munchobj[YOU].num = 1;
  148.   
  149.   jumping = 0; addy = 0;
  150.   anim = 2;
  151.   
  152.   timer = 0;
  153.   winittimer ();
  154.   wstarttimer (timerctr, TICKS(100));
  155.   do
  156.   {
  157.     spx = 0;
  158.     spy = 0;
  159.     ox = munchobj[YOU].x;
  160.     oy = munchobj[YOU].y;
  161.     
  162.     if  (jumping == 1)
  163.       addy += 2;
  164.     if  (addy > 15)
  165.       addy = 15;
  166.     
  167.     if  ((kbdon[CTRL]) & (jumping == 0))
  168.     {
  169.       jumping = 1;
  170.       addy = - 14;
  171.     }
  172.     
  173.     if  (kbdon[LEFT])
  174.     {
  175.       munchobj[YOU].x -= 8;
  176.       checkleft ();
  177.       if  (dir != 1)
  178.       {
  179.     dir = 1;
  180.     anim = 5;
  181.       }
  182.       anim++;
  183.       if  (anim > 8)
  184.     anim = 5;
  185.     }
  186.     else if  (kbdon[RIGHT])
  187.     {
  188.       munchobj[YOU].x += 8;
  189.       checkright ();
  190.       if  (dir != 2)
  191.       {
  192.     dir = 2;
  193.     anim = 1;
  194.       }
  195.       anim++;
  196.       if  (anim > 4)
  197.     anim = 1;
  198.     }
  199.     
  200.     munchobj[YOU].num = anim;
  201.     if  (munchobj[YOU].x == ox)
  202.       if  (dir == 1)
  203.     munchobj[YOU].num = 9;
  204.     else munchobj[YOU].num = 1;
  205.     
  206.     munchobj[YOU].y += addy;
  207.     if  (munchobj[YOU].y < 0) munchobj[YOU].y = 0;
  208.     if  (addy < 0)
  209.       checkhead ();
  210.     
  211.     
  212.     if  ((jumping == 1))
  213.       if  (dir == 1)
  214.     munchobj[YOU].num = 6;
  215.     else munchobj[YOU].num = 2;
  216.     
  217.     checkfeet ();
  218.     
  219.     spx = munchobj[YOU].x - worldx[MAINWIN] - windowmaxx[MAINWIN] / 2;
  220.     spy = munchobj[YOU].y - worldy[MAINWIN] - windowmaxy[MAINWIN] / 2;
  221.     
  222.     if  (kbdon[UP])
  223.     {
  224.       if  ((feet1 == 105) | (feet2 == 105))
  225.     upelev ();
  226.       else
  227.     spy = - 4;
  228.     }
  229.     if  (kbdon[DOWN])
  230.     {
  231.       if  ((feet1 == 105) | (feet2 == 105))
  232.     downelev ();
  233.       else
  234.     spy = + 4;
  235.     }
  236.     
  237.     checkelevators ();
  238.     // make sure they come back down when not standing on them
  239.     
  240.     moveguys ();
  241.     
  242.     wscrollwindow (MAINWIN, spx, spy);
  243.     wshowobjects (MAINWIN, 1, NUM_OBJ - 1, munchsprites, munchobj);
  244.     
  245.     wtextcolor (10);
  246.     wtexttransparent (TEXTFGBG);
  247.     wgtprintf (0, 0, NULL, "Free ram: %d", getmemfree ());
  248.     
  249.     wsetscreen (NULL);
  250.     wvesa_clip (0, 0, 639, 479);
  251.     wvesa_putblock (0, 0, scrollblock[MAINWIN], NORMAL);
  252.     wsetscreen (screenbuf);
  253.     updates++;
  254.   } while  (kbdon[ESC] != 1);          /* until ESC key is pressed */
  255.   wstoptimer ();  
  256.   wdonetimer ();
  257.  
  258.   uninstallkbd ();
  259.   
  260.   wendscroll (MAINWIN);
  261.   wfreesprites (munchtiles, 0, NUM_TILE - 1);
  262.   wfreesprites (munchsprites, 0, NUM_SPR - 1);
  263.   wfreemap (munchmap);
  264.   wsetmode (oldmode);
  265.   printf ("\nElapsed time (hundredths of seconds): %i", timer);
  266.   printf ("\n# updates: %d", updates);
  267.   printf ("\nMicroseconds per frame: %g", (float)(timer) / (float)(updates));
  268.   printf ("\nAverage frame rate: %2.2f frames/sec\n", (float)updates / ((float)(timer) / 100.0));
  269. }
  270.  
  271.  
  272. void checkright (void)
  273. {
  274.   short j,k;
  275.   short x;
  276.     
  277.   j = wgetworldblock (MAINWIN, munchobj[YOU].x + 16, munchobj[YOU].y + 1);
  278.   k = wgetworldblock (MAINWIN, munchobj[YOU].x + 16, munchobj[YOU].y + 15);
  279.   if  ((j >= 100) | (k >= 100))
  280.   {
  281.       x = munchobj[YOU].x / TILE_SIZE;
  282.       munchobj[YOU].x = x * TILE_SIZE;
  283.   }
  284. }
  285.  
  286.  
  287. void checkleft (void)
  288. {
  289.   short j,k;
  290.   short x;
  291.     
  292.   j = wgetworldblock (MAINWIN, munchobj[YOU].x, munchobj[YOU].y);
  293.   k = wgetworldblock (MAINWIN, munchobj[YOU].x, munchobj[YOU].y + 15);
  294.   if  ((j >= 100) | (k >= 100))
  295.   {
  296.       x = munchobj[YOU].x / TILE_SIZE;
  297.       x++;
  298.       munchobj[YOU].x = x * TILE_SIZE;
  299.   }
  300. }
  301.  
  302.  
  303. void checkfeet (void)
  304. {
  305.   short j,k,y;
  306.   
  307.   feet1 = wgetworldblock (MAINWIN, munchobj[YOU].x, munchobj[YOU].y + 16);
  308.   feet2 = wgetworldblock (MAINWIN, munchobj[YOU].x + 15, munchobj[YOU].y + 16);
  309.   if  ((feet1 < 50) & (feet2 < 50))
  310.     jumping = 1;
  311.   else 
  312.   {
  313.     y = munchobj[YOU].y / TILE_SIZE;
  314.     munchobj[YOU].y = y * TILE_SIZE;
  315.  
  316.     jumping = 0;
  317.     addy = 0;
  318.   }
  319. }
  320.  
  321.  
  322. void checkhead (void)
  323. {
  324.   short j,k,y;
  325.   
  326.   head1 = wgetworldblock (MAINWIN, munchobj[YOU].x, munchobj[YOU].y - 1);
  327.   head2 = wgetworldblock (MAINWIN, munchobj[YOU].x + 15, munchobj[YOU].y - 1);
  328.   if  ((head1 < 50) & (head2 < 50))
  329.     jumping = 1;
  330.   else 
  331.   {
  332.     y = munchobj[YOU].y / TILE_SIZE;
  333.     y++;
  334.     munchobj[YOU].y = y * TILE_SIZE;
  335.  
  336.     jumping = 0;
  337.     addy = 0;
  338.  
  339.   }
  340. }
  341.  
  342.  
  343. void findelevators (void)
  344. {
  345.   short i,j,k;
  346.   for  (i = 0; i <= mapheight[MAINWIN]; i++)
  347.     for  (j = 0; j <= mapwidth[MAINWIN]; j++)
  348.   {
  349.     k = wgetworldblock (MAINWIN, j*16, i*16);
  350.     if  (k == 105)
  351.     {
  352.       elev[numelev].curheight = i;
  353.       elev[numelev].origx = j;
  354.       elev[numelev].origy = i;
  355.       elev[numelev].timer = 0;
  356.       
  357.       for  (k = 0; k < 200; k++)
  358.     replace[k] = 0;
  359.       numelev++;
  360.     }
  361.   }
  362. }
  363.  
  364.  
  365. void upelev (void)
  366. {
  367.   short ii,jj;
  368.   for  (ii = 0; ii < numelev; ii++)
  369.   {
  370.     if  ((elev[ii].origx >= (munchobj[YOU].x / 16) - 1)
  371.     & (elev[ii].curheight >= (munchobj[YOU].y / 16) - 1)
  372.     & (elev[ii].origx <= (munchobj[YOU].x / 16) + 1)
  373.     & (elev[ii].curheight <= (munchobj[YOU].y / 16) + 1)
  374.     & ((elevup == - 1) | (elevup == ii))
  375.     & (munchobj[YOU].y > 16))
  376.     {
  377.       checkhead ();
  378.       if  ((head1 < 50) & (head2 < 50))
  379.       {
  380.     replace[elev[ii].curheight - 1] = wgetworldblock (MAINWIN, elev[ii].origx*16, (elev[ii].curheight - 1)*16);
  381.     wputworldblock (MAINWIN, elev[ii].origx*16, elev[ii].curheight*16, 104);
  382.     wputworldblock (MAINWIN, elev[ii].origx*16, (elev[ii].curheight - 1)*16, 105);
  383.     elev[ii].curheight--;
  384.     elevup = ii;
  385.     munchobj[YOU].y -= 16;
  386.     elev[ii].timer = 10;
  387.       }
  388.     }
  389.   }
  390. }
  391.  
  392.  
  393. void downelev (void)
  394. {
  395.   short ii,jj;
  396.   for  (ii = 0; ii < numelev; ii++)
  397.   {
  398.     if  ((elev[ii].origx >= (munchobj[YOU].x / 16) - 1)
  399.     & (elev[ii].curheight >= (munchobj[YOU].y / 16) - 1)
  400.     & (elev[ii].origx <= (munchobj[YOU].x / 16) + 1)
  401.     & (elev[ii].curheight <= (munchobj[YOU].y / 16) + 1)
  402.     & (elev[ii].curheight != elev[ii].origy))
  403.     {
  404.       wputworldblock (MAINWIN, elev[ii].origx*16, elev[ii].curheight*16, replace[elev[ii].curheight]);
  405.       wputworldblock (MAINWIN, elev[ii].origx*16, (elev[ii].curheight + 1)*16, 105);
  406.       elev[ii].curheight++;
  407.       if  (elev[ii].curheight == elev[ii].origy)
  408.     elevup = - 1;
  409.       munchobj[YOU].y += 16;
  410.       elev[ii].timer = 10;
  411.     }
  412.   }
  413. }
  414.  
  415.  
  416. void checkelevators (void)
  417. {
  418.   short ii;
  419.   
  420.   for  (ii = 0; ii < numelev; ii++)
  421.   {
  422.     if  ((elev[ii].curheight != elev[ii].origy))
  423.     {
  424.       if  (elev[ii].timer == 0)
  425.       {
  426.     wputworldblock (MAINWIN, elev[ii].origx*16, elev[ii].curheight*16, replace[elev[ii].curheight]);
  427.     wputworldblock (MAINWIN, elev[ii].origx*16, (elev[ii].curheight + 1)*16, 105);
  428.     elev[ii].curheight++;
  429.     if  (elev[ii].curheight == elev[ii].origy)
  430.       elevup = - 1;
  431.     elev[ii].timer = 0;
  432.       }
  433.       else elev[ii].timer--;
  434.     }
  435.   }
  436. }
  437.  
  438.  
  439. void moveguys (void)
  440. {
  441.   short j,k;
  442.   
  443.   for  (i = 0; i <= 36; i++)
  444.   {
  445.     if  (munchobj[i].on == 1) 
  446.       if (munchsprites[munchobj[i].num] != NULL)
  447.     // sprite made
  448.     {
  449.       if  (is_in_window (MAINWIN, munchobj[i].x, munchobj[i].y, 100))
  450.       {
  451.     if  (munchobj[i].num < 16)     // walking right
  452.     {
  453.       munchobj[i].num++;
  454.       if  (munchobj[i].num > 15) munchobj[i].num = 12;
  455.       // walking animation loop
  456.       munchobj[i].x += 3;
  457.       j = wgetworldblock (MAINWIN, munchobj[i].x + 16, munchobj[i].y + 16);
  458.       k = wgetworldblock (MAINWIN, munchobj[i].x + 16, munchobj[i].y + 8);
  459.       if  ((j < 50) | (k >= 50)) munchobj[i].num = 16;
  460.     }
  461.     if  (munchobj[i].num > 15)     // walking left
  462.     {
  463.       munchobj[i].num++;
  464.       if  (munchobj[i].num > 19) munchobj[i].num = 16;
  465.       // walking animation loop
  466.       munchobj[i].x -= 3;
  467.       j = wgetworldblock (MAINWIN, munchobj[i].x, munchobj[i].y + 16);
  468.       k = wgetworldblock (MAINWIN, munchobj[i].x, munchobj[i].y + 8);
  469.       if  ((j < 50) | (k >= 50)) munchobj[i].num = 12;
  470.     }
  471.       }
  472.     }
  473.   }
  474. }
  475.